from abc import ABCMeta, abstractmethod

# 抽象订阅者
class Users(metaclass=ABCMeta):
    @abstractmethod
    def update(self, notice):
        pass

# 抽象发布者
class Cortlcent:
    def __init__(self):
        self.observers = []

    def attach(self, obs):
        # 增加永华
        self.observers.append(obs)

    def detach(self, obs):
        # 删除
        self.observers.remove(obs)

    def notify(self):
        # 推送更新
        for obs in self.observers:
            obs.update(self)

# 具体发布者
class Duyueming(Cortlcent):
    def __init__(self, centel_info):
        super().__init__()
        # 创建为私有属性
        self.__centel_info = centel_info
    # 属性装饰器语法
    @property
    def centel_info(self):
        return self.__centel_info
    # 负责重写方法
    @centel_info.setter
    def centel_info(self, info):
        self.__centel_info = info # 实现自己更新
        self.notify() # 推送


class Used(Users):
    def __init__(self):
        self.centel_info = None

    def update(self, notice):
        self.centel_info = notice.centel_info


notice = Duyueming("公司信息")
# 订阅者
s1 = Used()
s2 = Used()
# 绑定订阅者
notice.attach(s1)
notice.attach(s2)
# 发布信息
notice.centel_info = "信息已发布"
print(s1.centel_info)
# 取消订阅
notice.detach(s2)
notice.centel_info = "无信息发布"
print(s1.centel_info)
print(s2.centel_info)